package com.ruoyi.supplier.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.supplier.domain.bo.ProductSpecRelBo;
import com.ruoyi.supplier.domain.vo.ProductSpecRelVo;
import com.ruoyi.supplier.domain.ProductSpecRel;
import com.ruoyi.supplier.mapper.ProductSpecRelMapper;
import com.ruoyi.supplier.service.IProductSpecRelService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 商品与规格值关系记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-18
 */
@RequiredArgsConstructor
@Service
@DS("slave")
public class ProductSpecRelServiceImpl implements IProductSpecRelService {

    private final ProductSpecRelMapper baseMapper;

    /**
     * 查询商品与规格值关系记录
     */
    @Override
    public ProductSpecRelVo queryById(Integer specRelId){
        return baseMapper.selectVoById(specRelId);
    }

    /**
     * 查询商品与规格值关系记录列表
     */
    @Override
    public TableDataInfo<ProductSpecRelVo> queryPageList(ProductSpecRelBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ProductSpecRel> lqw = buildQueryWrapper(bo);
        Page<ProductSpecRelVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商品与规格值关系记录列表
     */
    @Override
    public List<ProductSpecRelVo> queryList(ProductSpecRelBo bo) {
        LambdaQueryWrapper<ProductSpecRel> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ProductSpecRel> buildQueryWrapper(ProductSpecRelBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ProductSpecRel> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getProductId() != null, ProductSpecRel::getProductId, bo.getProductId());
        lqw.eq(bo.getSpecId() != null, ProductSpecRel::getSpecId, bo.getSpecId());
        lqw.eq(bo.getSpecValueId() != null, ProductSpecRel::getSpecValueId, bo.getSpecValueId());
        lqw.eq(bo.getMerchantId() != null, ProductSpecRel::getMerchantId, bo.getMerchantId());
        return lqw;
    }

    /**
     * 新增商品与规格值关系记录
     */
    @Override
    public Boolean insertByBo(ProductSpecRelBo bo) {
        ProductSpecRel add = BeanUtil.toBean(bo, ProductSpecRel.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setSpecRelId(add.getSpecRelId());
        }
        return flag;
    }

    /**
     * 修改商品与规格值关系记录
     */
    @Override
    public Boolean updateByBo(ProductSpecRelBo bo) {
        ProductSpecRel update = BeanUtil.toBean(bo, ProductSpecRel.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ProductSpecRel entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商品与规格值关系记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
